#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import datetime
import json
from typing import Iterable, Any

import scrapy
from scrapy import Request
from scrapy.http import Response

from apps.tax_policy_declaration.tax_policy_declaration.items import NetTaxPolicyDeclarationItem
from utils.tools import to_date

POLICT_TYPE = [
    {
        "order": 1,
        "name": "碳达峰",
        "id": "K35770832",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "TDF",
        "$multiValueField": None,
    },
    {
        "order": 2,
        "name": "企业资质认定与奖励",
        "id": "K35770834",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "ZZJL",
        "$multiValueField": None,
    },
    {
        "order": 3,
        "name": "高增长/独角兽",
        "id": "K35771914",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "GZZ",
        "$multiValueField": None,
    },
    {
        "order": 4,
        "name": "转型转产",
        "id": "K35770837",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "ZXZC",
        "$multiValueField": None,
    },
    {
        "order": 5,
        "name": "贷款贴息贴保",
        "id": "K35724617",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "LIDB",
        "$multiValueField": None,
    },
    {
        "order": 6,
        "name": "中小微企业",
        "id": "K35771920",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "LittleMicro",
        "$multiValueField": None,
    },
    {
        "order": 7,
        "name": "产业联盟",
        "id": "K32194528",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "CCIA",
        "$multiValueField": None,
    },
    {
        "order": 8,
        "name": "节能减排",
        "id": "K35724611",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "ESAER",
        "$multiValueField": None,
    },
    {
        "order": 9,
        "name": "企业培训",
        "id": "K35771911",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "PX",
        "$multiValueField": None,
    },
    {
        "order": 10,
        "name": "社会组织",
        "id": "K32194527",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "NGO",
        "$multiValueField": None,
    },
    {
        "order": 11,
        "name": "上市/并购",
        "id": "K35770840",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "SSPY",
        "$multiValueField": None,
    },
    {
        "order": 12,
        "name": "科研立项",
        "id": "K35724584",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "Srtp",
        "$multiValueField": None,
    },
    {
        "order": 13,
        "name": "商贸物流",
        "id": "K35771919",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "CITL",
        "$multiValueField": None,
    },
    {
        "order": 14,
        "name": "房租水电补贴",
        "id": "K35770839",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "FZBT",
        "$multiValueField": None,
    },
    {
        "order": 15,
        "name": "纳税奖励",
        "id": "K35770836",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "NS",
        "$multiValueField": None,
    },
    {
        "order": 16,
        "name": "中介服务",
        "id": "K32194523",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "Agency",
        "$multiValueField": None,
    },
    {
        "order": 17,
        "name": "股权资助",
        "id": "K35724616",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "EquityFinancing",
        "$multiValueField": None,
    },
    {
        "order": 18,
        "name": "品牌/市场开拓",
        "id": "K32194525",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "BAMD",
        "$multiValueField": None,
    },
    {
        "order": 19,
        "name": "消费促进",
        "id": "K35771912",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "XFCJ",
        "$multiValueField": None,
    },
    {
        "order": 20,
        "name": "活动策划",
        "id": "K32194524",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "EventsPlanner",
        "$multiValueField": None,
    },
    {
        "order": 21,
        "name": "信息化/工业互联网",
        "id": "K35724612",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "InformationAndIntegration",
        "$multiValueField": None,
    },
    {
        "order": 22,
        "name": "稳企稳岗",
        "id": "K35770838",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "WQWG",
        "$multiValueField": None,
    },
    {
        "order": 23,
        "name": "抗疫政策",
        "id": "K35771915",
        "source": "查策网",
        "whetherDeleted": "是",
        "code": "KYZC",
        "$multiValueField": None,
    },
    {
        "order": 24,
        "name": "传统产业",
        "id": "K35771918",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "Industries",
        "$multiValueField": None,
    },
    {
        "order": 25,
        "name": "创新载体",
        "id": "K32194522",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "InnovativePlatform",
        "$multiValueField": None,
    },
    {
        "order": 26,
        "name": "事前资助",
        "id": "K35724614",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "ExAnteFunding",
        "$multiValueField": None,
    },
    {
        "order": 27,
        "name": "新基建",
        "id": "K35770833",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "XJJ",
        "$multiValueField": None,
    },
    {
        "order": 28,
        "name": "事后资助",
        "id": "K35724613",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "AfterTheFund",
        "$multiValueField": None,
    },
    {
        "order": 29,
        "name": "科技成果奖励",
        "id": "K35724618",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "SATA",
        "$multiValueField": None,
    },
    {
        "order": 30,
        "name": "专精特新",
        "id": "K35770835",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "ZJTX",
        "$multiValueField": None,
    },
    {
        "order": 31,
        "name": "产业化",
        "id": "K35724588",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "Manufacturing",
        "$multiValueField": None,
    },
    {
        "order": 32,
        "name": "标准化",
        "id": "K35724619",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "Standardization",
        "$multiValueField": None,
    },
    {
        "order": 33,
        "name": "扩产上规模",
        "id": "K35724589",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "ScaleUp",
        "$multiValueField": None,
    },
    {
        "order": 34,
        "name": "大型企业",
        "id": "K35724581",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "LargeEnterprises",
        "$multiValueField": None,
    },
    {
        "order": 35,
        "name": "招商引资",
        "id": "K32194526",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "Attract",
        "$multiValueField": None,
    },
    {
        "order": 36,
        "name": "重大项目",
        "id": "K35724585",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "MajorProject",
        "$multiValueField": None,
    },
    {
        "order": 37,
        "name": "高新技术企业",
        "id": "K35771916",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "High-Tech",
        "$multiValueField": None,
    },
    {
        "order": 38,
        "name": "应用示范",
        "id": "K35724587",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "AppliedModel",
        "$multiValueField": None,
    },
    {
        "order": 39,
        "name": "知识产权",
        "id": "K35724620",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "IPR",
        "$multiValueField": None,
    },
    {
        "order": 40,
        "name": "人才认定与资助",
        "id": "K32194521",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "TIAF",
        "$multiValueField": None,
    },
    {
        "order": 41,
        "name": "新兴产业",
        "id": "K35771917",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RisingEconomy",
        "$multiValueField": None,
    },
    {
        "order": 42,
        "name": "配套资助",
        "id": "K35724615",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "BySupport",
        "$multiValueField": None,
    },
    {
        "order": 43,
        "name": "研发中试",
        "id": "K35724586",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RADOP",
        "$multiValueField": None,
    },
    {
        "order": 44,
        "name": "技术改造",
        "id": "K35724590",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "TechnicalReform",
        "$multiValueField": None,
    },
    {
        "order": 45,
        "name": "总部企业",
        "id": "K35724582",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "HQHeadquarte",
        "$multiValueField": None,
    },
    {
        "order": 46,
        "name": "产业基金",
        "id": "K32194529",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "ManufachuringMoney",
        "$multiValueField": None,
    },
    {
        "order": 47,
        "name": "创新创业",
        "id": "K35771913",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "CXCY",
        "$multiValueField": None,
    },
    {
        "order": 48,
        "name": "研发资助",
        "id": "K35724583",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "LaunchAid",
        "$multiValueField": None,
    },
    {
        "order": None,
        "name": "低空经济",
        "id": "ZZ36518265",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "DKJJ",
        "$multiValueField": None,
    },
    {
        "order": None,
        "name": "低空经济",
        "id": "ZZ36518266",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "DKJJ",
        "$multiValueField": None,
    },
    {
        "order": None,
        "name": "专项资金",
        "id": "ZZ36631999",
        "source": "clickpaas",
        "whetherDeleted": "否",
        "code": "ZXZJ",
        "$multiValueField": None,
    },
]
IND_LIST = [
    {
        "name": "金融业",
        "id": "K32193562",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "42",
        "$multiValueField": None,
    },
    {
        "name": "电子竞技产业",
        "id": "K32193387",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "76",
        "$multiValueField": None,
    },
    {
        "name": "航空航天",
        "id": "K32193561",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "24",
        "$multiValueField": None,
    },
    {
        "name": "公共服务",
        "id": "K35770981",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "39",
        "$multiValueField": None,
    },
    {
        "name": "高技术服务",
        "id": "K32193564",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "51",
        "$multiValueField": None,
    },
    {
        "name": "文化创意设计",
        "id": "K32193565",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "47",
        "$multiValueField": None,
    },
    {
        "name": "电子信息",
        "id": "K32193390",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "20",
        "$multiValueField": None,
    },
    {
        "name": "传统实业",
        "id": "K32193385",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "75",
        "$multiValueField": None,
    },
    {
        "name": "新能源汽车",
        "id": "K35723684",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "26",
        "$multiValueField": None,
    },
    {
        "name": "汽车制造业",
        "id": "K32193383",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "78",
        "$multiValueField": None,
    },
    {
        "name": "物联网",
        "id": "K32193389",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "104",
        "$multiValueField": None,
    },
    {
        "name": "生命健康",
        "id": "K35723685",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "29",
        "$multiValueField": None,
    },
    {
        "name": "医疗器械",
        "id": "K35723687",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "27",
        "$multiValueField": None,
    },
    {
        "name": "农业",
        "id": "K32193568",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "44",
        "$multiValueField": None,
    },
    {
        "name": "节能环保",
        "id": "K35723682",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "32",
        "$multiValueField": None,
    },
    {
        "name": "生物医药",
        "id": "K35723686",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "28",
        "$multiValueField": None,
    },
    {
        "name": "电子商务",
        "id": "K35770958",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "54",
        "$multiValueField": None,
    },
    {
        "name": "新材料",
        "id": "K35770959",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "50",
        "$multiValueField": None,
    },
    {
        "name": "机器人",
        "id": "K35723688",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "30",
        "$multiValueField": None,
    },
    {
        "name": "新能源与节能",
        "id": "K35723683",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "49",
        "$multiValueField": None,
    },
    {
        "name": "化工产业",
        "id": "K35770960",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "200",
        "$multiValueField": None,
    },
    {
        "name": "建筑业",
        "id": "K32193566",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "80",
        "$multiValueField": None,
    },
    {
        "name": "集成电路",
        "id": "K35770951",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "86",
        "$multiValueField": None,
    },
    {
        "name": "区块链",
        "id": "K35770953",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "94",
        "$multiValueField": None,
    },
    {
        "name": "云计算",
        "id": "K35770956",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "90",
        "$multiValueField": None,
    },
    {
        "name": "交通/物流/供应链",
        "id": "K32193563",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "41",
        "$multiValueField": None,
    },
    {
        "name": "贸易",
        "id": "K35770984",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "40",
        "$multiValueField": None,
    },
    {
        "name": "其他",
        "id": "K35770985",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "48",
        "$multiValueField": None,
    },
    {
        "name": "科研单位",
        "id": "K35770982",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "38",
        "$multiValueField": None,
    },
    {
        "name": "采矿业",
        "id": "K32193384",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "79",
        "$multiValueField": None,
    },
    {
        "name": "生态修复",
        "id": "K35723681",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "33",
        "$multiValueField": None,
    },
    {
        "name": "大数据",
        "id": "K35770954",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "100",
        "$multiValueField": None,
    },
    {
        "name": "互联网",
        "id": "K35770957",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "22",
        "$multiValueField": None,
    },
    {
        "name": "军工业",
        "id": "K35770983",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "53",
        "$multiValueField": None,
    },
    {
        "name": "科技中介",
        "id": "K32193569",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "71",
        "$multiValueField": None,
    },
    {
        "name": "软件",
        "id": "K35770952",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "84",
        "$multiValueField": None,
    },
    {
        "name": "5G/移动通信",
        "id": "K32193388",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "102",
        "$multiValueField": None,
    },
    {
        "name": "产业园",
        "id": "K32193567",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "82",
        "$multiValueField": None,
    },
    {
        "name": "传统服务业",
        "id": "K32193570",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "43",
        "$multiValueField": None,
    },
    {
        "name": "人工智能",
        "id": "K35770955",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "96",
        "$multiValueField": None,
    },
    {
        "name": "现代服务业",
        "id": "K32193386",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "77",
        "$multiValueField": None,
    },
    {
        "name": "高端装备",
        "id": "K35723689",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "31",
        "$multiValueField": None,
    },
    {
        "name": "海洋产业",
        "id": "K35723690",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "25",
        "$multiValueField": None,
    },
    {
        "name": "低空经济",
        "id": "ZZ36165646",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "201",
        "$multiValueField": None,
    },
    {
        "name": "低空经济",
        "id": "ZZ36165647",
        "order": None,
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "201",
        "$multiValueField": None,
    },
    {
        "name": "家具制造业",
        "id": "ZZ36734667",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "通用设备制造业",
        "id": "ZZ36734677",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "广播、电视、电影和录音制作业",
        "id": "ZZ36734672",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "未来能源",
        "id": "ZZ36734639",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "房地产",
        "id": "ZZ36734648",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "食品加工",
        "id": "ZZ36734662",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "教育",
        "id": "ZZ36734642",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "娱乐业",
        "id": "ZZ36734650",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "土木工程建筑业",
        "id": "ZZ36734663",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "印刷",
        "id": "ZZ36734665",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "未来空间",
        "id": "ZZ36734640",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "未来健康",
        "id": "ZZ36734637",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "新闻和出版业",
        "id": "ZZ36734671",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "造纸和纸制品业",
        "id": "ZZ36734666",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "农副食品加工业",
        "id": "ZZ36734675",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "橡胶和塑料制品业",
        "id": "ZZ36734654",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "文化艺术业",
        "id": "ZZ36734649",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "绿色低碳",
        "id": "ZZ36734634",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "纺织业",
        "id": "ZZ36734668",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "酒、饮料和精制茶制造业",
        "id": "ZZ36734674",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "飞机制造",
        "id": "ZZ36734630",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "先进材料",
        "id": "ZZ36734631",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "未来智能",
        "id": "ZZ36734638",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "电力、热力生产和供应业",
        "id": "ZZ36734658",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "燃气生产和供应业",
        "id": "ZZ36734659",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "烟草制品业",
        "id": "ZZ36734656",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "金属制品业",
        "id": "ZZ36734670",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "文化艺术创作",
        "id": "ZZ36734673",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "建筑安装业",
        "id": "ZZ36734664",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "未来材料",
        "id": "ZZ36734641",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "住宿和餐饮业",
        "id": "ZZ36734647",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "化学原料和化学制品制造业",
        "id": "ZZ36734676",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "畜牧业",
        "id": "ZZ36734645",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "软件和信息技术服务业",
        "id": "ZZ36734646",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "林业",
        "id": "ZZ36734643",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "体育",
        "id": "ZZ36734652",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "煤炭",
        "id": "ZZ36734657",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "水的生产和供应业",
        "id": "ZZ36734660",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "仪器仪表制造业",
        "id": "ZZ36734679",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "智能终端",
        "id": "ZZ36734636",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "计算机、通信和其他电子设备制造业",
        "id": "ZZ36734678",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "渔业",
        "id": "ZZ36734644",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "交通运输业",
        "id": "ZZ36734653",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "时尚消费品",
        "id": "ZZ36734632",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "石油和天然气开采业",
        "id": "ZZ36734655",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "旅游",
        "id": "ZZ36734651",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "服装",
        "id": "ZZ36734669",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "元宇宙",
        "id": "ZZ36734635",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "数字经济",
        "id": "ZZ36734633",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "核产业",
        "id": "ZZ36734661",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "公共管理",
        "id": "ZZ36734692",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "批发业",
        "id": "ZZ36734680",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "社会组织",
        "id": "ZZ36734694",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "电信、广播电视和卫星传输服务",
        "id": "ZZ36734682",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "互联网和相关服务",
        "id": "ZZ36734683",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "保险业",
        "id": "ZZ36734684",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "公共设施管理业",
        "id": "ZZ36734689",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "零售业",
        "id": "ZZ36734681",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "社会保障",
        "id": "ZZ36734693",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "居民服务业",
        "id": "ZZ36734690",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "租赁业",
        "id": "ZZ36734687",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "货币金融服务",
        "id": "ZZ36734685",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "社会工作",
        "id": "ZZ36734691",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "生态保护和环境治理业",
        "id": "ZZ36734688",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
    {
        "name": "资本市场服务",
        "id": "ZZ36734686",
        "order": None,
        "source": "其他",
        "whetherDeleted": "否",
        "code": None,
        "$multiValueField": None,
    },
]
AREA = [
    {
        "name": "国家部委",
        "order": 1,
        "id": "K32190838",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_",
    },
    {
        "name": "上海市",
        "order": 2,
        "id": "K32190839",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai",
    },
    {
        "name": "宝山区",
        "order": 3,
        "id": "K32190983",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_BaoShanQu",
    },
    {
        "name": "奉贤区",
        "order": 4,
        "id": "K32190989",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_FengXianQu",
    },
    {
        "name": "闵行区",
        "order": 5,
        "id": "K32190982",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_MinXingQu",
    },
    {
        "name": "静安区",
        "order": 6,
        "id": "K35721078",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_JingAnQu",
    },
    {
        "name": "杨浦区",
        "order": 7,
        "id": "K32190981",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_YangPuQu",
    },
    {
        "name": "浦东新区",
        "order": 8,
        "id": "K32190985",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_PuDongXinQu",
    },
    {
        "name": "松江区",
        "order": 9,
        "id": "K32190987",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_SongJiangQu",
    },
    {
        "name": "徐汇区",
        "order": 10,
        "id": "K35721076",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_XuHuiQu",
    },
    {
        "name": "长宁区",
        "order": 11,
        "id": "K35721077",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_ChangNingQu",
    },
    {
        "name": "青浦区",
        "order": 12,
        "id": "K32190988",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_QingPuQu",
    },
    {
        "name": "黄浦区",
        "order": 13,
        "id": "K35721075",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_HuangPuQu",
    },
    {
        "name": "虹口区",
        "order": 14,
        "id": "K35721080",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_HongKouQu",
    },
    {
        "name": "普陀区",
        "order": 15,
        "id": "K35721079",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_PuTuoQu",
    },
    {
        "name": "嘉定区",
        "order": 16,
        "id": "K32190984",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_JiaDingQu",
    },
    {
        "name": "崇明区",
        "order": 17,
        "id": "K32190990",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_ChongMingXian",
    },
    {
        "name": "金山区",
        "order": 18,
        "id": "K32190986",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_JinShanQu",
    },
    {
        "name": "中国（上海）自由贸易试验区",
        "order": 19,
        "id": "K35721071",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_SHZYMYSYQ",
    },
    {
        "name": "中国（上海）自由贸易试验区临港新片区",
        "order": 20,
        "id": "K32633395",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_LinGangDQ",
    },
    {
        "name": "张江科学城",
        "order": 21,
        "id": "K32190840",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_Zjkxc",
    },
    {
        "name": "上海化学工业区",
        "order": 22,
        "id": "K35721074",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_HuaXueGYQ",
    },
    {
        "name": "上海张江国家自主创新示范区",
        "order": 23,
        "id": "K35721072",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_ZJGXJS",
    },
    {
        "name": "上海虹桥国际中央商务区",
        "order": None,
        "id": "ZZ44254074",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_HQGJSWQ",
    },
    {
        "name": "上海虹桥国际中央商务区",
        "order": None,
        "id": "ZZ44254075",
        "source": "查策网",
        "whetherDeleted": "否",
        "code": "RegisterArea_ZXS_Shanghai_HQGJSWQ",
    },
]


class ShQyfwTaxPolicyDeclarationSpider(scrapy.Spider):
    name = "sh_qyfw_tax_policy_declaration"
    source = "上海市企业服务云"
    province = "上海市"
    level = "市级"
    polict_type = POLICT_TYPE
    ind_list = IND_LIST
    area = AREA
    datas = []

    custom_settings = {
        "ITEM_PIPELINES": {
            "apps.tax_policy_declaration.tax_policy_declaration.pipelines.TaxPolicyDeclarationPipeline": 300,
        },
    }

    def start_requests(self) -> Iterable[Request]:
        url = "https://shpolicy.ssme.sh.gov.cn/governmentCloudApi/chatSNet/policy"
        for industryType in self.ind_list:
            for area in self.area:
                for projectType in self.polict_type:
                    data = {
                        "pageNum": 1,
                        "pageSize": 10,
                        "area": area.get("name"),
                        "industryType": industryType.get("name"),
                        "projectType": projectType.get("name"),
                        "informationSource": None,
                        "applicationStatus": None,
                        "name": "",
                    }
                    yield Request(
                        url=url,
                        method="POST",
                        body=json.dumps(data),
                        callback=self.parse_list,
                        headers={
                            "Content-Type": "application/json;charset=UTF-8",
                        },
                        cb_kwargs={
                            "area": area.get("name"),
                            "industryType": industryType.get("name"),
                            "projectType": projectType.get("name"),
                        },
                    )

    def parse_list(self, response, **kwargs):
        yield from self.parse(response, **kwargs)
        # url = "https://shpolicy.ssme.sh.gov.cn/governmentCloudApi/chatSNet/policy"
        # params = json.loads(response.request.body.decode())
        # resp = response.json()
        # total = resp["data"]["respData"]["total"]
        # if int(params["pageNum"]) * 10 < int(total):
        #     yield Request(
        #         url=url,
        #         method="POST",
        #         body=json.dumps({**params, "pageNum": int(params["pageNum"]) + 1}),
        #         callback=self.parse_list,
        #         headers={
        #             "Content-Type": "application/json;charset=UTF-8",
        #         },
        #     )

    def parse(self, response: Response, **kwargs: Any) -> Any:
        resp = response.json()
        records = resp["data"]["respData"]["dataList"]
        for record in records:
            application_deadline = record.get("endDate")
            valid = 1
            level = "市级"
            if "区" in kwargs.get("area"):
                level = "区级"
            if application_deadline:
                if to_date(application_deadline) < datetime.datetime.now():
                    valid = 0
            item = NetTaxPolicyDeclarationItem(
                **{
                    "enjoyment_subject": None,  # 享受主体
                    "enjoyment_condition": record.get("condition"),  # 申报条件
                    "policy_industry": (
                        ";".join(json.loads(record.get("industryType"))) if record.get("industryType") else None
                    ),
                    "application_materials": record.get("material"),  # 申报该项目的所需材料
                    "declaration_start_time": record.get("startDate"),
                    "application_deadline": application_deadline,
                    "source": self.source,
                    "province": self.province,
                    "policy_theme": kwargs.get("projectType"),
                    "city": self.province,  # 城市
                    "county": record.get("area"),  # 区县
                    "application_area": record.get("area"),  # 区县
                    "max_support_amount": record.get("fund"),  # 扶持最大金额
                    "project_name": record.get("name"),  # 项目名称
                    "release_time": record.get("createdTime"),
                    "department": record.get("informationSource"),
                    "policy_basis": record.get("announcements"),
                    "content": record.get("support"),
                    "source_url": "https://shpolicy.ssme.sh.gov.cn/knowledge/#/detail/" + record.get("id"),
                    "administrative_level": level,  # 行政层级
                    "handling_process": None,  # 办理流程
                    "charging_situation": None,  # 收费情况
                    "processing_time": None,
                    "legal_time_limit": None,
                    "commitment_time_limit": None,
                    "declaration_address": None,  # 具体申报的线下地址
                    "consultant": None,  # 咨询申报项目的人员
                    "hotline": None,
                    "complaints_hotline": None,
                    "valid": valid,
                }
            )
            url = f"https://shpolicy.ssme.sh.gov.cn/governmentCloudApi/chatSNet/policyInfo?policyId={record.get('id')}&type=%E7%94%B3%E6%8A%A5%E9%80%9A%E7%9F%A5"
            yield Request(
                url=url,
                method="GET",
                callback=self.parse_detail,
                meta={"item": item},
            )

    def parse_detail(self, response, **kwargs):
        item = response.meta["item"]
        resp = response.json()
        if resp["code"] == 200:
            data_list = resp["data"]["respData"]["dataList"]
            if data_list:
                item["policy_sunnary"] = data_list[0]["content"]
                # item['declaration_type'] = data_list[0]["qualification"]
        yield item


if __name__ == "__main__":
    from scrapy import cmdline

    cmdline.execute("scrapy crawl sh_qyfw_tax_policy_declaration".split())
